package com.fywsy.fyweb.strategy;

import com.fywsy.fyweb.entity.SendItem;
import com.fywsy.fyweb.utils.JsonUtil;
import com.fywsy.fyweb.utils.RestTemplateUtil;
import com.fywsy.fyweb.utils.TextUtils;
import com.google.gson.Gson;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.HashMap;
import java.util.Map;

/**
 * 十三道
 *
 *
 * 114.66.45.140
 * http://114.66.45.140:3030/mail/add
 * 游戏后台实现类
 * 注解gameId对应数据库游戏id
 * 注解GongnengMethod对应数据库功能id
 */
@Component
@GameId(1)
public class ShisandaoStrategy extends BaseGameStrategy {

    public static final String URL = "http://114.66.45.140:3030/mail/add";

    public static final String LOGIN = "http://114.66.45.140:4030/s_index/login";

    public static final String CLEAR_BAG = "http://114.66.45.140:4030/s_player/editSave/%s/%s/user_%s/act_actItem_1";

    public static final String CLEAR_MAIL = "http://114.66.45.140:4030/s_player/editSave/%s/%s/user_%s/mail_mailList_list";

    @GongnengMethod(1)
    public String mail(SendItem item) {
        //{"uuid":"100003","token":"000","version":"0.1.94","time":"1737438754","params":"[[1,901,3000]]"}
        HashMap<String,String> params = new HashMap<>();
        params.put("uuid",item.uid);
        params.put("token","000");
        params.put("version","0.1.94");
        params.put("time","1737438754");
        String ss = "[[1," + item.itemId + "," + item.number + "]]";
        params.put("params",ss);

        String result = RestTemplateUtil.sendPostJson(URL, params);
        String type = JsonUtil.getValueByKey(result, "type");
        if("1".equals(type)) {
            return "发送成功,重启到账邮件！";
        }
        return "发送失败";
    }

    @GongnengMethod(4)
    public String largeMail(SendItem item) {
        return mail(item);
    }

    @GongnengMethod(2)
    public String claerBag(SendItem item) {
        MultiValueMap<String,String> loginParam = new LinkedMultiValueMap<>();
        loginParam.add("username","admin");
        loginParam.add("password","123456");
        String homeResp = RestTemplateUtil.sendPostForm(LOGIN, loginParam);
        String token = getToken(homeResp);
        if(TextUtils.isEmpty(token)) {
            return "操作异常，联系客服~";
        }
        MultiValueMap<String,String> clearParam = new LinkedMultiValueMap<>();
        Map<String,Long> items = new HashMap<>();
        items.put("1",100000000l);//灵石
        items.put("2",50000000000l);//铜币
        items.put("8",100000000l);//代金券
        clearParam.add("info",new Gson().toJson(items));
        String clearResp = RestTemplateUtil.sendPostForm(String.format(CLEAR_BAG, token, item.uid, item.uid), clearParam);
        return !clearResp.contains("重新登陆") ? "操作成功,请重新上线" : "操作失败，联系客服~";
    }

    @GongnengMethod(3)
    public String clearMail(SendItem item) {
        MultiValueMap<String,String> loginParam = new LinkedMultiValueMap<>();
        loginParam.add("username","admin");
        loginParam.add("password","123456");
        String homeResp = RestTemplateUtil.sendPostForm(LOGIN, loginParam);
        String token = getToken(homeResp);
        if(TextUtils.isEmpty(token)) {
            return "操作异常，联系客服~";
        }
        MultiValueMap<String,String> clearParam = new LinkedMultiValueMap<>();
        clearParam.add("info","{\"139\":{\"id\":139,\"type\":1,\"title\":\"gm\",\"content\":\"gm到账\",\"items\":[[1,1,1]],\"fts\":1739986112,\"rts\":1740019859,\"ets\":1739980800,\"dts\":1741276800}}");
        String clearResp = RestTemplateUtil.sendPostForm(String.format(CLEAR_MAIL, token, item.uid, item.uid), clearParam);
        return !clearResp.contains("重新登陆") ? "操作成功,请重新上线" : "操作失败，联系客服~";
    }


    /**
     * Redirecting to <a href="/s_user/814879cd004a5ea66ed0470055e72a2e">/s_user/814879cd004a5ea66ed0470055e72a2e</a>.
     * @param input
     */
    private String getToken(String input) {
        if(TextUtils.isEmpty(input)) return null;
        int end = input.lastIndexOf("<");
        if(end < 0) return null;
        String substring = input.substring(0, end);
        int begin = substring.lastIndexOf("/");
        if(begin < 0) return null;
        return substring.substring(begin + 1);

    }

    public static void main(String[] args) {
        SendItem item = new SendItem();
        item.uid = "100003";
        item.number = "11";
        item.itemId = "51";
        System.out.println(new ShisandaoStrategy().claerBag(item));
    }
}

